package com.cbgolf.oa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.MessageBean;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.widget.ExpandableTextView;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/3.
 */

public class MessageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<MessageBean> list = new ArrayList<>();
    private onMessageListener listener;
    private Context context;

    public MessageAdapter(Context context, onMessageListener listener) {
        this.mInflater = LayoutInflater.from(context);
        this.listener = listener;
        this.context = context;
    }

    public void clear() {
        if (null != list && list.size() > 0) {
            this.list.clear();
        }
    }

    public void addALL(List<MessageBean> list) {
        this.list.addAll(list);
    }

    public void addData(List<MessageBean> list) {
        this.list.addAll(list);
    }


    public void setData(List<MessageBean> ls) {
        this.list = ls;
    }

    public void upDataUI() {
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder h = null;
        if (null == view) {
            h = new Holder();
            view = mInflater.inflate(R.layout.item_msg, null);
            h.item = view.findViewById(R.id.item_msg_layout_ll);
            h.msgTypeTv = (TextView) view.findViewById(R.id.item_msg_type_tv);
            h.typeAndTimeTv = (TextView) view.findViewById(R.id.item_msg_type_and_time_tv);
            h.genderTv = (TextView) view.findViewById(R.id.item_msg_gender_tv);
            h.userTypeTv = (TextView) view.findViewById(R.id.item_msg_user_type_tv);
            h.stateTv = (TextView) view.findViewById(R.id.item_msg_state_tv);
            h.contentTv = (ExpandableTextView) view.findViewById(R.id.expandable_text);

            h.modifyTv = (TextView) view.findViewById(R.id.item_msg_modify_tv);
            h.delTv = (TextView) view.findViewById(R.id.item_msg_del_tv);
            h.yesNoView = view.findViewById(R.id.item_msg_yes_orno_ll);
            view.setTag(h);
        } else {
            h = (Holder) view.getTag();
        }
        AutoUtil.auto(view);

        MessageBean m = list.get(position);
        if (null == m) {
            return view;
        }
        String sendType = m.pushTimeType;
        String time = m.pushTime;
        String gender = m.gender;
        String state = m.send;
        String[] userTypes = m.identityNames;

        String content = m.messageContent;
        if (!Util.isNull(state) && "true".equals(state)) {
            h.stateTv.setText("已推送");
            h.yesNoView.setVisibility(View.GONE);
        } else {
            h.stateTv.setText("未推送");
            h.yesNoView.setVisibility(View.VISIBLE);
        }
        h.typeAndTimeTv.setText(EnumUtil.getSendType(sendType) + "  " + DateUtil.stampToDate(time, DateUtil.formatWithbias));
        h.genderTv.setText(EnumUtil.getGender(gender));
        String userType = "";
        if (null != userTypes && userTypes.length > 0) {
            for (int i = 0; i < userTypes.length; i++) {
                if (i < userTypes.length - 1) {
                    userType += userTypes[i] + "、";
                } else {
                    userType += userTypes[i];
                }
            }
        }
        h.userTypeTv.setText(userType);
//        h.contentTv.setTextSize(DensityUtil.sp2px(context,12));
        h.contentTv.setText(Util.isNull(content)?"":content, true);
        h.modifyTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.modify(position);
                }
            }
        });
        h.delTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.del(position);
                }
            }
        });
        return view;
    }

    public interface onMessageListener {
        void modify(int position);

        void del(int position);
    }

    class Holder {
        private View item;
        private TextView msgTypeTv;
        private TextView typeAndTimeTv;
        private TextView genderTv;
        private TextView userTypeTv;
        private TextView stateTv;
        private ExpandableTextView contentTv;
        private TextView openTv;
        private TextView modifyTv;
        private TextView delTv;
        private View yesNoView;

    }
}
